package com.ldk.base.framework.datasource;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
//import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class CustomSqlSessionManager {
	
	private static final Log logger = LogFactory.getLog(CustomSqlSessionManager.class); 

	private Map<String, SqlSessionFactory> factories;
	
	private static CustomSqlSessionManager instance;
	public static CustomSqlSessionManager getInstance(){
		synchronized (CustomSqlSessionManager.class) {
			if(instance == null){
				instance = new CustomSqlSessionManager();
			}
			return instance;
		}
	}
	
	private CustomSqlSessionManager(){
		factories = new HashMap<String, SqlSessionFactory>();
	}
	
	/**
	 * 添加数据源
	 * @param key
	 * @param dataSource
	 */
	public synchronized void addDataSource(String key, DataSource dataSource){
		if(dataSource == null || StringUtils.isEmpty(key)) return;
		try{
			if(!factories.containsKey(key)){
				SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
				factoryBean.setDataSource(dataSource);
				PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
				factoryBean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
				factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/*Mapper.xml"));
				SqlSessionFactory factory = factoryBean.getObject();
				factories.put(key, factory);
			}
		}
		catch(MalformedURLException e){
			logger.error("CustomSqlSessionManager.addDataSource::"+e.getMessage());
		}
		catch(Exception e){
			logger.error("CustomSqlSessionManager.addDataSource::"+e.getMessage());
		}
	}
	
	/**
	 * 添加新的SqlSessionFactory
	 * @param key
	 * @param factory
	 */
	public synchronized void addSqlSessionFactory(String key, SqlSessionFactory factory){
		if(factory == null || StringUtils.isEmpty(key))
			return;
		if(factories == null)
			factories = new HashMap<String, SqlSessionFactory>();
		if(!factories.containsKey(key)){
			factories.put(key, factory);
		}
	}
	
	/**
	 * 创建新的SqlSession
	 * @param key
	 * @return
	 */
	public SqlSession newSqlSession(String key){
		if(factories == null || factories.isEmpty())
			return null;
		SqlSessionFactory factory = factories.get(key);
		if(factory == null){
			return null;
		}
		try{
			return SqlSessionUtils.getSqlSession(factory);
		}
		catch(Exception e){
			logger.error("CustomSqlSessionManager.Exception::"+e.getMessage());
		}
		return null;
	}
	
	/**
	 * 是否已经存在数据源
	 * @param key
	 * @return
	 */
	public Boolean existDataSource(String key){
		if(factories == null || factories.isEmpty())
			return false;
		return factories.containsKey(key);
	}
}
